//
//  RQActionSheetCell.m
//  RQActionSheet
//
//  Created by RocQ on 2017/6/5.
//  Copyright © 2017年 fengchao. All rights reserved.
//

#import "RQActionSheetCell.h"

#define kScreenWidth   ([UIScreen mainScreen].bounds.size.width)

@interface RQActionSheetCell()

@property (nonatomic, strong) UIButton *titleButton;
@property (nonatomic, weak) UIView *topLine; ///< 顶部线条
@property (nonatomic, weak) UIImageView *checkedView;
@end

@implementation RQActionSheetCell

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted) {
        self.contentView.backgroundColor = RQActionSheetColorWithString(RQActionSheetRowHighlightedColor);
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            self.contentView.backgroundColor = self.backgroundColor;
        }];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
    
    self.backgroundColor = RQActionSheetColorWithString(RQActionSheetRowNormalColor);
    self.contentView.backgroundColor = self.backgroundColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _contentAlignment = RQContentAlignmentCenter;
    
    [self setupSubviews];
    
    return self;
}


- (void)setupSubviews {
  
    _titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _titleButton.tintColor = [UIColor blackColor];
    _titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _titleButton.userInteractionEnabled = NO;
    _titleButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:_titleButton];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_titleButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleButton)]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_titleButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleButton)]];
    
    UIView *topLine = [[UIView alloc] init];
    topLine.backgroundColor = RQActionSheetColorWithString(RQActionSheetRowTopLineColor);
    topLine.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:topLine];
    self.topLine = topLine;
    CGFloat lineHeight = 1/[UIScreen mainScreen].scale; ///< 线条高度
    CGFloat labelMargin = RQActionSheetDefaultMargin;

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLine(lineHeight)]" options:0 metrics:@{@"lineHeight":@(lineHeight)} views:NSDictionaryOfVariableBindings(topLine)]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-labelMargin-[topLine]-labelMargin-|" options:0 metrics:@{@"labelMargin":@(labelMargin)} views:NSDictionaryOfVariableBindings(topLine)]];
    
    UIImageView *checkedView = [[UIImageView alloc]init];
    checkedView.image = [UIImage imageNamed:@"right"];
    checkedView.contentMode = UIViewContentModeScaleAspectFit;
    checkedView.hidden = YES;
    [self.contentView addSubview:checkedView];
    self.checkedView = checkedView;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.checkedView.frame = CGRectMake(self.bounds.size.width - 20 - 12, 12 ,20, 20);
}

- (void)setTextStr:(NSString *)textStr
{
    _textStr = textStr;
    [_titleButton setTitle:textStr forState:UIControlStateNormal];
}

- (void)setContentAlignment:(RQContentAlignment)contentAlignment {
    if (_contentAlignment == contentAlignment) return;
    
    _contentAlignment = contentAlignment;

    // 设置内容偏移
    switch (_contentAlignment) {
            // 局左
        case RQContentAlignmentLeft: {
            _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            _titleButton.contentEdgeInsets = UIEdgeInsetsMake(0, RQActionSheetDefaultMargin, 0, -RQActionSheetDefaultMargin);
            break;
        }
            // 居中
        case RQContentAlignmentCenter: {
            _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            _titleButton.contentEdgeInsets = UIEdgeInsetsZero;
            break;
        }
            // 居右
        case RQContentAlignmentRight: {
            _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            _titleButton.contentEdgeInsets = UIEdgeInsetsMake(0, -RQActionSheetDefaultMargin, 0, RQActionSheetDefaultMargin);
            break;
        }
    }
}

- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    
    self.checkedView.hidden = !isSelected;
}

@end
